function Role(){
	this.grid = null;
	//this.InitGrid()
	this.table=this.InitTable();
	this.tab=this.InitTab();
	
	this.form=this.InitEleUI();
	
	
}

Role.prototype.InitEleUI=function(){
	var form;
	layui.use('form', function(){
       form = layui.form; //只有执行了这一步，部分表单元素才会自动修饰成功
	   form.render();
	});  
	return form;
}
Role.prototype.InitTab=function(){
	var element;
	layui.use('element', function(){
	   element = layui.element;
	});
	return element;
}

Role.prototype.OpTab=function(tabindex,obj){
	this.tab.tabChange('tabcontent', tabindex);
	if(obj){
		 document.formedit.rolename.value=obj.rolename;
		 document.formedit.remark.value=obj.remark;
		 document.formedit.roleid.value=obj.roleid;
		 $("input[name='roletype']").removeAttr("checked");
		 $("input[name='roletype']").eq(obj.roletype).attr("checked","");
		 this.form.render();
	}
}

Role.prototype.InitTable = function() {
	var table;
	var _this=this;
	layui.use('table', function() {
	    table = layui.table;
		table.render({
			elem : '#list'
			,height : $(window).height() - 140
			,url : 'get_list' // 数据接口
			,page : true // 开启分页
			,limit : 40
			,id: 'rolelist'
			,method:"post"
		    ,limits:[20,40,80,100]
			,cols : [ [ // 表头
			           {type:'numbers'}
			           ,{field : 'rolename',title : '角色名称',width : 200,align : 'center'}
			           //, {field : 'roletype',title : '类型',width : 120,align : 'center'}
			           //, {field : 'createtime',title : '时间',width : 170,align : 'center'}
			           , {field : 'remark',title : '描述',width : 240,align : 'center'}
			           , {field : 'usercount',title : '绑定人数',width : 100,align : 'center'}
			           , {field : 'menucount',title : '绑定菜单数',width : 120,align : 'center'}
			           , {title : '操作',width : 130,align : 'center',toolbar : '#operate'}
		            ] ]
		    ,request:{
				type:'post'
			}
		    ,even: true //开启隔行背景
			,where:{rolename: $("#rolename").val()}
		    ,done: function(res, page, count){
		    	$("[data-field='roletype']").children().each(function(){  
                    if($(this).text()=='0'){  
                       $(this).text("普通民警")  
                    }
                    else if($(this).text()=='1'){  
                        $(this).text("部门管理员")  
                     }
                    else if($(this).text()=='2'){  
                       $(this).text("系统管理员")  
                    }
               })  
		    }
		    
		});

		
		 //监听工具条
	    table.on('tool(list)', function(obj){
	      var data = obj.data;
	      if(obj.event === 'detail'){
	        layer.msg('ID：'+ data.id + ' 的查看操作');
	      } else if(obj.event === 'del'){
	    	  
	        top.mylayui.confirm('确定要删除吗？',{icon: 0}, function(index,dd){
	        	 top.mylayui.close(index);
	        	 vRole.deleteentity(data.roleid);
	        });
	      } else if(obj.event === 'edit'){
	        //layer.alert('编辑行：<br>'+ JSON.stringify(data))
	    	  _this.OpTab(2,data);
	      }
	      else if(obj.event=='setrights'){
	    	  vRole.openadd(data.roleid);
	      }
	    });

	    
//	    var  active = {
//	      reload:function(){
//	    	  table.reload('rolelist', { 
//	    		  page: {
//	    	          curr: 1 //重新从第 1 页开始
//	    	        }
//	    	      ,where: {  
//	    	    	  rolename: $("#rolename").val()  
//	    	      }  
//	    	});  
//	      }
//	    }
//	    
//	    layui.$('#btnquery').on('click', function(){ 
//	        var type = 'reload';  
//	        active[type] ? active[type].call(this) : '';  
//	    }); 
//		

	});
	
	return table;
}





Role.prototype.InitGrid=function (){
	var _this = this;
    window.liger.remove("list");
    if (_this.grid == null) {
        _this.grid = $("#list").ligerGrid({
            columns: [{ display: '序号', width: 80, name: 'rowNum' },
                     { display: '角色名称', width: 120, name: 'rolename'},
                     { display: '类型', width: 120, name: 'roletype', render: function (rowdata, rowindex, value) {
                    	if(value==1){
                    		return "系统角色";
                    	}
                    	else{
                    		return "项目角色";
                    	}
                     } },
                     //{ display: '从属项目', width: 150, name: 'projectname' },
                     { display: '描述', width: 200, name: 'remark' },
                     { display: '创建时间', width: 130, name: 'createtime', render: function (rowdata, rowindex, value) {
                    	return value;
                     } },
                     {display: '操作', width: 270, name: 'userid', render: function (rowdata, rowindex, value) {
                    	return '';
                     }}
            ],
            pageSize: 15,
            url: "get_list" + '?time=' + new Date().getTime(),
            delayLoad: false,            //初始化时是否加载,为true时,不加载,为false时,则加载
            sortName: null,
            dataAction: 'server',
            parms: {
                Method: "",
                rolename: $("#rolename").val()
            },
            sortOrder: null,
            sourceObj: _this.grid,
            root: 'items',                       //数据源字段名
            record: 'countRecord',                    //数据源记录数字段名
            pageParmName: 'CurrentPage',               //页索引参数名，(提交给服务器)
            pagesizeParmName: 'PageSize',        //页记录数参数名，(提交给服务器)
            data: {},
            width: "100%",
            height: '100%',
            heightDiff: -120,
            onSuccess: function (rowdata, rowindex) { if (rowdata.status > 0) { alert(rowdata.msg) } },
            alternatingRow: true,
            border: 0
        })
    }
}

Role.prototype.searchlist = function () {
    this.grid.setOptions({
        newPage: 1, parms: {
            Method: "",
            rolename: $("#rolename").val()
        }
    });
    this.grid.loadData();
}

Role.prototype.searchtable=function(){
	this.table.reload('rolelist', { 
		  page: {
	        curr: 1 //重新从第 1 页开始
	      }
	    ,where: {  
	  	  rolename: $("#rolename").val()  
	    }  
	}); 
}

Role.prototype.deleteentity=function(roleid){
	$.ajax({
		type:"POST",
		url:"delete_role",
		data:{roleid:roleid},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				vRole.searchtable();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}


Role.prototype.openadd=function(roleid){
	top.mylayui.open({
         type: 2,
         title: '权限设置',
         shadeClose: true,
         shade: 0.5,
         area: ['800px', '500px'],
         content: 'role/role_rights?roleid='+roleid
       }); 

	var iframeele=top.$(".layui-layer-content").find('iframe');
	iframeele.css({"height":$(iframeele).height()-10});
}



Role.prototype.addrolemenu=function(roleid,menuids){
	$.ajax({
		type:"POST",
		url:"insert_rolemenu",
		data:{roleid:roleid,menuids:menuids},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				top.mylayui.closeAll('iframe');
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}

Role.prototype.addroleorg=function(roleid,orgids){
	$.ajax({
		type:"POST",
		url:"insert_roleorg",
		data:{roleid:roleid,orgids:orgids},
		beforeSend:function(XHR){
			
		},
		success:function(data){
			data=eval('('+data+')');
			if(data.result>0){
				top.mylayui.msg(data.msg, {icon: 1});
				top.mylayui.closeAll('iframe');
				document.location.reload();
			}
			else{
				top.mylayui.msg(data.msg, {icon: 0});
			}
		},
		complete:function(XHR){
			
		},
		error:function(){
			
		}
	});
}


var vRole;
var vTable;
$(function () {
	vRole = new Role();
})


function CloseDialog(result,msg){
	if(result>0){
		top.mylayui.msg('操作成功', {icon: 1});
		document.formedit.reset();
		vRole.searchtable();
		vRole.OpTab(1);
	}
	else{
		if(msg){
			top.mylayui.msg(msg, {icon: 0});
		}else{
			top.mylayui.msg('操作失败', {icon: 0});
		}
	}
}